﻿Imports System.Configuration

Public Class frmMain

    Dim dtArtists As DataTable
    Dim dtAlbums As DataTable

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadData()

    End Sub

    Private Sub ReloadDatabaseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ReloadDatabaseToolStripMenuItem.Click
        LoadData()

    End Sub

    Private Sub ForceGrooveAppToCloseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ForceGrooveAppToCloseToolStripMenuItem.Click
        ForceCloseGroove()
        LoadData()

    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()

    End Sub

    Private Sub GrooveDatabaseInfoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GrooveDatabaseInfoToolStripMenuItem.Click
        MsgBox(GetDatabasePath)

    End Sub

    Private Sub ArtistsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ArtistsToolStripMenuItem.Click
        ShowTableData(dtArtists)

    End Sub

    Private Sub AlbumsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AlbumsToolStripMenuItem.Click
        ShowTableData(dtAlbums)

    End Sub

    Private Sub LoadData()
        Dim path As String

        Try
            path = GetDatabasePath()

            dtArtists = ESE.GetTable(path, "tblPerson")
            dtAlbums = ESE.GetTable(path, "tblAudioAlbum")

            lstArtists.DataSource = dtArtists

            ResetAlbums(New DataView(dtAlbums))

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

    End Sub

    Private Sub lstArtists_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstArtists.SelectedIndexChanged
        Try
            Me.Cursor = Cursors.WaitCursor
            Application.DoEvents()

            FilterAlbums()

        Finally
            Me.Cursor = Cursors.Default

        End Try

    End Sub

    Private Sub lstArtists_DoubleClick(sender As Object, e As EventArgs) Handles lstArtists.DoubleClick
        Try
            Me.Cursor = Cursors.WaitCursor
            Application.DoEvents()

            OpenArtist()

        Finally
            Me.Cursor = Cursors.Default

        End Try

    End Sub

    Private Sub lstArtists_DrawItem(sender As Object, e As DrawItemEventArgs) Handles lstArtists.DrawItem
        Dim dr As DataRowView
        Dim foreColor As Brush
        Dim fnt As Font
        Dim txt As String
        Dim drawArea As Rectangle

        If e.Index >= 0 Then
            dr = DirectCast(lstArtists.Items(e.Index), DataRowView)
            foreColor = SystemBrushes.WindowText
            fnt = New Font(e.Font, CType(IIf(IsDBNull(dr("ServiceMediaID")), FontStyle.Bold, FontStyle.Regular), FontStyle))
            txt = CStr(dr(lstArtists.DisplayMember))
            drawArea = e.Bounds

            With e
                .DrawBackground()
                .Graphics.DrawString(txt, fnt, foreColor, drawArea)
                .DrawFocusRectangle()
            End With
        End If

    End Sub

    Private Sub lstAlbums_DoubleClick(sender As Object, e As EventArgs) Handles lstAlbums.DoubleClick
        Try
            Me.Cursor = Cursors.WaitCursor
            Application.DoEvents()

            OpenAlbum()

        Finally
            Me.Cursor = Cursors.Default

        End Try
    End Sub

    Private Sub ResetAlbums(dv As DataView)
        With lstAlbums
            .DataSource = Nothing
            .ValueMember = "ID"
            .DisplayMember = "Title"
            .DataSource = dv
        End With

    End Sub

    Private Sub ForceCloseGroove()
        Dim apps() As Process

        apps = Process.GetProcessesByName("WWAHost")

        If apps.Length > 0 Then
            apps(0).Kill()
            MsgBox("The Groove Music process is now closed.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)

        Else
            MsgBox("The Groove Music process was not found.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation)

        End If

    End Sub

    Private Sub FilterAlbums()
        Dim dr As DataRowView
        Dim dv As DataView

        lstAlbums.DataSource = Nothing
        If lstArtists.SelectedItem IsNot Nothing Then
            dr = DirectCast(lstArtists.SelectedItem, DataRowView)
            dv = New DataView(dtAlbums, String.Format("ArtistID={0}", CStr(dr("ID"))), "", DataViewRowState.CurrentRows)

        Else
            dv = New DataView(dtAlbums)
        End If

        ResetAlbums(dv)

    End Sub

    Private Sub OpenArtist()
        Dim f As frmArtist
        Dim dr As DataRowView

        If lstArtists.SelectedItem IsNot Nothing Then
            dr = DirectCast(lstArtists.SelectedItem, DataRowView)
            f = New frmArtist(dr)
            With f
                .ShowDialog(Me)
                .Dispose()
            End With

        End If

    End Sub

    Private Sub OpenAlbum()
        Dim f As frmAlbum
        Dim dr As DataRowView

        If lstAlbums.SelectedItem IsNot Nothing Then
            dr = DirectCast(lstAlbums.SelectedItem, DataRowView)
            f = New frmAlbum(dr)
            With f
                .ShowDialog(Me)
                .Dispose()
            End With

        End If

    End Sub

    Private Sub ShowTableData(table As DataTable)
        Dim f As frmTableViewer

        f = New frmTableViewer(table)
        f.Show()

    End Sub

    Friend Function GetDatabasePath() As String
        Dim path As String
        Dim userPaths() As String

        path = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ConfigurationManager.AppSettings("dbPath"))

        If Not IO.Directory.Exists(path) Then
            Throw New IO.DirectoryNotFoundException("Groove database folder has not been created.  Run Groove Music first to create a database." & vbCrLf & "Path: " & path)
        End If

        userPaths = IO.Directory.GetDirectories(path)

        If userPaths.Length = 0 Then
            Throw New IO.DirectoryNotFoundException("Groove database subfolder has not been created.  Run Groove Music first to create a database." & vbCrLf & "Path: " & path)
        End If

        path = userPaths.First

        If String.IsNullOrEmpty(path) Then
            Throw New IO.DirectoryNotFoundException("Groove database has not been created.  Run Groove Music first to create a database." & vbCrLf & "Path: " & path)
        End If

        path = IO.Path.Combine(path, "EntClientDb.edb")

        Return path

    End Function

End Class
